package com.example.administrator.yltmusic;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class MyService extends Service {
    public MyService() {
    }

    private String[] music={"eight.mp3","elevent.mp3","five.mp3","four.mp3","nine.mp3","one.mp3","seven.mp3","six.mp3","ten.mp3","three.mp3","two.mp3"};
    private MediaPlayer mediaPlayer;
    private Timer t;
    public static int y;

    //绑定服务时,调用此方法
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {

        return new MusicControl();
    }

    //创建播放音乐的服务
    @Override
    public void onCreate() {
        super.onCreate();

        //创建音乐播放器对象
        mediaPlayer = new MediaPlayer();
    }

    //销毁播放音乐服务
    @Override
    public void onDestroy() {
        super.onDestroy();

        //停止播放音乐
        mediaPlayer.stop();

        //释放占用的资源
        mediaPlayer.release();

        //将player置为空
        mediaPlayer = null;
    }

    //播放音乐
    public void play() {

        try {

            if(mediaPlayer == null)
            {
                mediaPlayer = new MediaPlayer();
            }

            //重置
            mediaPlayer.reset();

            //加载多媒体文件
            mediaPlayer.setDataSource("sdcard/"+music[y]);

            //准备播放音乐
            mediaPlayer.prepare();

            //播放音乐
            mediaPlayer.start();

            //添加计时器
            addTimer();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //暂停播放音乐
    public void pause() {

        mediaPlayer.pause();
    }

    //继续播放音乐
    public void Play() {

        mediaPlayer.start();
    }
    //上一首
    public void last(){

        if (y>0){
            y=y-1;
        }else{
            y=10;
        }

        try {

            if(mediaPlayer == null)
            {
                mediaPlayer = new MediaPlayer();
            }

            //重置
            mediaPlayer.reset();

            //加载多媒体文件
            mediaPlayer.setDataSource("sdcard/"+music[y]);

            //准备播放音乐
            mediaPlayer.prepare();

            //播放音乐
            mediaPlayer.start();

            //添加计时器
            addTimer();

        } catch (IOException e) {

            e.printStackTrace();
        }
    }
    //下一首

    public void firit(){

        if (y<10){
            y=y+1;
        }else{
            y=0;
        }

        try {

            if(mediaPlayer == null)
            {
                mediaPlayer = new MediaPlayer();
            }

            //重置
            mediaPlayer.reset();

            //加载多媒体文件
            mediaPlayer.setDataSource("sdcard/"+music[y]);

            //准备播放音乐
            mediaPlayer.prepare();

            //播放音乐
            mediaPlayer.start();

            //添加计时器
            addTimer();

        } catch (IOException e) {

            e.printStackTrace();
        }
    }

    //创建一个实现音乐接口的音乐控制类
    class MusicControl extends Binder implements Music {

        @Override
        public void play() {

            MyService.this.play();
        }

        @Override
        public void last() {

            MyService.this.last();
        }

        @Override
        public void firit() {

            MyService.this.firit();
        }

        @Override
        public void pause() {

            MyService.this.pause();
        }

        @Override
        public void Play() {

            MyService.this.Play();
        }

        @Override
        public void seekTo(int progress) {

            MyService.this.seekTo(progress);
        }


    }

    //设置音乐的播放位置
    public void seekTo(int progress) {

        mediaPlayer.seekTo(progress);
    }

    //添加计时器用于设置音乐播放器中的播放进度
    public void addTimer() {

        //如果没有创建计时器对象
        if(t == null) {

            //创建计时器对象
            t = new Timer();

            t.schedule(new TimerTask() {

                               //执行计时任务
                               @Override
                               public void run() {

                                   //获得歌曲总时长
                                   int duration =mediaPlayer.getDuration();

                                   //获得歌曲的当前播放进度
                                   int currentPosition = mediaPlayer.getCurrentPosition();

                                   //创建消息对象
                                   Message msg = palyActivity.handler.obtainMessage();

                                   //将音乐的播放进度封装至消息对象中
                                   Bundle bundle = new Bundle();
                                   bundle.putInt("duration", duration);
                                   bundle.putInt("currentPosition", currentPosition);
                                   msg.setData(bundle);

                                   //将消息发送到主线程的消息队列
                                   palyActivity.handler.sendMessage(msg);
                               }
                           },

                    //开始计时任务后的5毫秒，第一次执行run方法，以后每500毫秒执行一次
                    5, 500);
        }
    }

}